package design.pattern.combination;

import java.util.ArrayList;
import java.util.List;

/**
 * 树枝结点
 *
 * @author AtoVio, Winter Yuan
 */
public class Menu extends MenuComponent {

    // 菜单可以有多个子菜单和子菜单项
    private final List<MenuComponent> menuComponentList = new ArrayList<>();

    public Menu(String name, int level) {
        super(name, level);
    }

    @Override
    public void add(MenuComponent component) {
        menuComponentList.add(component);
    }

    @Override
    public void remove(MenuComponent component) {
        menuComponentList.remove(component);
    }

    @Override
    public MenuComponent getChild(int index) {
        return menuComponentList.get(index);
    }

    @Override
    public void print() {
        for (int i = 0; i < level; i++) {
            System.out.print("--");
        }
        // 打印名称
        System.out.println(name);

        // 打印子菜单和子菜单项
//        for (MenuComponent menuComponent : menuComponentList) {
//            menuComponent.print();
//        }
        menuComponentList.forEach(MenuComponent::print);
    }
}
